package com.xiaomi.back.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaomi.back.service.advertisement.AdvertisementBackService;
import com.xiaomi.pojo.Advertisement;
import com.xiaomi.pojo.User;
import com.xiaomi.util.ToolsUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@PropertySource("classpath:config.properties")
public class AdvertisementController {
    @Autowired
    private AdvertisementBackService advertisementBackService;

    @Value("${upload.path}")
    private String uploadPath ;

    //查询全部广告
    @RequestMapping("/selectAd")
    public String selectAd(Model model, @RequestParam(defaultValue = "1") int currentPageNo) {
        PageHelper.startPage(currentPageNo, 9);
        List<Advertisement> advertisements = advertisementBackService.getADV(8);
        PageInfo<Advertisement> page=new PageInfo<>(advertisements);
        model.addAttribute("page", page);

        return "back/Advertisement/AdverAll";
    }

    //按类型 查询广告
    @RequestMapping("/getAdvlx")
    public String selectAd(Model model, @RequestParam(defaultValue = "1") int currentPageNo,@RequestParam("lx")int lx) {
        PageHelper.startPage(currentPageNo, 9);
        List<Advertisement> advertisements = advertisementBackService.getADV(lx);
        PageInfo<Advertisement> page=new PageInfo<>(advertisements);
        model.addAttribute("page", page);

        model.addAttribute("lx",lx);
        return "back/Advertisement/getAdv";
    }

    //根据 aid 进入修改广告页
    @RequestMapping("/UpAdver/{aid}")
    public String UpAdver(@PathVariable("aid")int aid,Model model){
        model.addAttribute("aid",aid);
        return "back/Advertisement/AdvFrom";
    }

    //修改广告图片
    @PostMapping("/upAdv")
    @ResponseBody
    public Map UpAdvers(int cid, int aid, MultipartFile uploadFile,Model model){

        Map result = new HashMap() ;
        //先获取文件名称
        String fileName = uploadFile.getOriginalFilename() ;

            //修改文件名 新文件名+原文件后缀 xxxx.jpg
            String fileNewName = ToolsUtil.getFileNewName(fileName);

            File upFile = new File(uploadPath, fileNewName) ;

            //文件目录是否存在
            if(!upFile.exists()) {
                upFile.mkdirs() ;
            }

            try {
                uploadFile.transferTo(upFile);
                int count=advertisementBackService.UpADV(fileNewName,cid,aid);
                result.put("state",true);
                result.put("count",count);
            } catch (IOException e) {

                result.put("state",false);
                e.printStackTrace();
            }
            return result;
        }
}
